<?php
//error_reporting(~E_WARNING);
date_default_timezone_set('Asia/Kolkata');
session_start();
//////////// Getting file name /////
$array = explode("/", $_SERVER['SCRIPT_NAME']);
$fileName = array_pop($array);
define('FILE_NAME', $fileName);
/**************Mysql Connectivity ********/
$username = 'root';
$password = '';
$hostname = 'localhost';
$dbName = 'b18';

//Mysql Login / connection
mysql_connect($hostname, $username, $password) or die("<span style=\"color:red\">Unable to connect MYSQL</span>" . mysql_error());

// Db Select
mysql_select_db($dbName) or die("<span style=\"color:red\">Unable to select Database</span>" . mysql_error());
/****************************************/

function pr($arr = array()) {
  echo '<pre style="background:black;color:white;">';
  print_r($arr);
  echo '</pre>';
}

function vx($arr = array()) {
  $flag = true;
  if ($flag == false)
    return;
  echo '<pre style="color:white;background:black;">';
  var_export($arr);
  echo '</pre>';
}

/*
 * DB function used to insert data in specific Db Table
 * @param $tableName STRING (Name of Table)
 * @param $daata Array (Data to be inserted)
 * @return INT (last insert ID)
 * Usage : echo insert('users',$_POST);
 * */

function insert($tableName = '', $data = array()) {

  if (empty($tableName) || empty($data)) {
    return "Unable to insert";
  }
  $insertQuery = "INSERT INTO `$tableName` SET";

  foreach ($data as $colName => $value) {
    $insertQuery .= "`$colName`='$value' ,";
  }

  $insertQuery = rtrim($insertQuery, ', ');

  mysql_query($insertQuery) or die(mysql_error() . " <br>Unable to INSERT DATA");

  return mysql_insert_id();
  /*
   // How to select LAst inserted KEY
   echo "<br>last Insert Id is :".mysql_insert_id();

   // How to know number of rows affected
   echo "<br>Affected Rows :".mysql_affected_rows();
   */
}

/*
 @param STRING $table = 'users'
 @param STRING $where = "WHERE `name`='php'"
 @param STRING $order = 'ORDER BY `name` DESC'
 @param STRING $limit = 'LIMIT 0,1'
 @param STRING $colName = '`id`,`name`,`email`'
 @return $result STREAM(resource)
 *
 */
function fetchAll($table = '', $where = '', $order = '', $limit = '', $colName = '') {

  $colName = ($colName == '') ? '*' : $colName;
  $query = "SELECT $colName FROM `$table` $where $order $limit";
  pr($query);
  $result = mysql_query($query) or die(mysql_error());
  return $result;
}

function getCountries($key = '') {
  $countryArray = array("in" => "India", "pk" => "pakistan", "np" => "Nepal", "ch" => "China", "bn" => "BanglaDesh", "sl" => "SriLanka", "bh" => "Bhutan", "af" => "Afganistan", "my" => "Myanmar");

  if ($key != "") {
    return $countryArray[$key];
  } else {
    return $countryArray;
  }
}//end getCountries

function getStatus($status = 1) {
  //if($status==1){
  //return 'Active';
  //} else {
  //return 'Inactive';
  //}

  return ($status == 1) ? 'Active' : 'Inactive';
}

function getGender($gender = 0) {
  return ($gender == 1) ? 'Female' : 'Male';
}
